package com.fortune.service.impl;

import com.fortune.dao.BackReturnOrderDao;
import com.fortune.dto.in.BackReturnOrder.ReturnOrderAddDto;
import com.fortune.dto.in.BackReturnOrder.ReturnOrderDetailAddDto;
import com.fortune.dto.in.BackReturnOrder.ReturnOrderDetailDto;
import com.fortune.dto.in.BackReturnOrder.ReturnOrderEditDto;
import com.fortune.exception.db.DeleteInnerErrorException;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.BackReturnOrderService;
import com.fortune.vo.BackReturnOrder.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BackReturnOrderServiceImpl implements BackReturnOrderService {
    @Autowired
    private BackReturnOrderDao backReturnOrderDao;

    /**
     * 后台经销商退押金工单列表
     * @param did
     * @return
     */
    @Override
    public List<ReturnOrder> findReturnOrderList(String did, ReturnOrderListDto returnOrderListDto) {
        return backReturnOrderDao.findReturnOrderList(did, returnOrderListDto);
    }

    /**
     * 后台经销商退押金工单列表总条数
     * @param did
     * @return
     */
    @Override
    public Integer returnOrderListCount(String did, ReturnOrderListDto returnOrderListDto) {
        return backReturnOrderDao.returnOrderListCount(did, returnOrderListDto);
    }

    /**
     * 查询开单人是经销商的名字
     * @param createUser
     * @param did
     * @return
     */
    @Override
    public String findJXSName(String createUser, String did) {
        return backReturnOrderDao.findJXSName(createUser, did);
    }

    /**
     * 查询开单人是客户的名字
     * @param createUser
     * @return
     */
    @Override
    public String findKHName(String createUser) {
        return backReturnOrderDao.findKHName(createUser);
    }

    /**
     * 批准人名字
     * @param allowUser
     * @param did
     * @return
     */
    @Override
    public String findKDRName(String allowUser, String did) {
        return backReturnOrderDao.findKDRName(allowUser, did);
    }

    /**
     * 司机名字
     * @param boxDriver
     * @param did
     * @return
     */
    @Override
    public String findSJName(String boxDriver, String did) {
        return backReturnOrderDao.findSJName(boxDriver, did);
    }

    /**
     * 库房验收人
     * @param storeUser
     * @param did
     * @return
     */
    @Override
    public String findKFName(String storeUser, String did) {
        return backReturnOrderDao.findKFName(storeUser, did);
    }

    /**
     * 查询退押金工单详情
     * @param did
     * @param clientId
     * @param orderId
     * @return
     */
    @Override
    public List<ReturnOrderDetail> findReturnOrderDetailList(String did, String clientId, String orderId) {
        return backReturnOrderDao.findReturnOrderDetailList(did, clientId, orderId);
    }

    /**
     * 添加退押金工单
     * @param did
     * @param id
     * @param returnOrderAddDto
     * @return
     */
    @Transactional
    @Override
    public int addReturnOrder(String did, String id, ReturnOrderAddDto returnOrderAddDto) {
        int addReturnOrder = backReturnOrderDao.addReturnOrder(did, id, returnOrderAddDto);
        if(addReturnOrder < 1){
            throw new InsertInnerErrorException("添加失败");
        }
        String clientId = returnOrderAddDto.getClientId();
        List<ReturnOrderDetailDto> returnOrderDetailDtos = returnOrderAddDto.getReturnOrderDetailDto();
        for(ReturnOrderDetailDto returnOrderDetailDto : returnOrderDetailDtos){
            int addReturnOrderDetail = backReturnOrderDao.addReturnOrderDetail(did, id, clientId, returnOrderDetailDto);
            if(addReturnOrderDetail < 1){
                throw new InsertInnerErrorException("添加失败");
            }
        }
        return addReturnOrder;
    }

    /**
     * 单独添加详情
     * @param did
     * @param returnOrderDetailAddDto
     * @return
     */
    @Override
    public int addRedturnOrderDetails(String did, ReturnOrderDetailAddDto returnOrderDetailAddDto) {
        return backReturnOrderDao.addRedturnOrderDetails(did, returnOrderDetailAddDto);
    }

    /**
     * 编辑退押金工单
     * @param did
     * @param returnOrderEditDto
     * @return
     */
    @Transactional
    @Override
    public int editReturnOrder(String did, ReturnOrderEditDto returnOrderEditDto) {
        int editReturnOrder = backReturnOrderDao.editReturnOrder(did, returnOrderEditDto);
        if(editReturnOrder < 1){
            throw new UpdateInnerErrorException("编辑失败");
        }
        int deleteReturnOrderDetails = backReturnOrderDao.deleteReturnOrderDetails(did, returnOrderEditDto.getClientId(), returnOrderEditDto.getOrderId());
        if (deleteReturnOrderDetails < 1){
            throw new DeleteInnerErrorException("删除详情失败");
        }
        List<ReturnOrderDetailDto> returnOrderDetailDtos = returnOrderEditDto.getReturnOrderDetailDto();
        for(ReturnOrderDetailDto returnOrderDetailDto : returnOrderDetailDtos){
            int addReturnOrderDetail = backReturnOrderDao.addReturnOrderDetail(did, returnOrderEditDto.getOrderId(), returnOrderEditDto.getClientId(), returnOrderDetailDto);
            if (addReturnOrderDetail < 1){
                throw new UpdateInnerErrorException("编辑详情失败");
            }
        }
        return editReturnOrder;
    }

    /**
     * 删除退押金工单
     * @param did
     * @param orderId
     * @return
     */
    @Override
    public int deleteRrturnOrder(String did, String orderId, String clientId) {
        return backReturnOrderDao.deleteRrturnOrder(did, orderId, clientId);
    }

    /**
     * 批准退押金
     * @param did
     * @param orderId
     * @return
     */
    @Override
    public int approvedReturnOrder(String did, String orderId,String id,String driverId) {
        return backReturnOrderDao.approvedReturnOrder(did, orderId,id,driverId);
    }

    /**
     * 获取客户列表
     * @param did
     * @return
     */
    @Override
    public List<ClientList> findClientList(String did) {
        return backReturnOrderDao.findClientList(did);
    }

    /**
     * 获取客户手机号
     * @param clientId
     * @return
     */
    @Override
    public String findClientPhone(String clientId) {
        return backReturnOrderDao.findClientPhone(clientId);
    }

    @Override
    public List<Product> findProductListByD(String did) {
        return backReturnOrderDao.findProductListByD(did);
    }

    @Override
    public List<MID> findMid(String did) {
        return backReturnOrderDao.findMid(did);
    }

    @Override
    public List<Product> findProductListByM(String mid) {
        return backReturnOrderDao.findProductListByM(mid);
    }

    /**
     * 查询当前客户购买过的商品
     * @param did
     * @param clientId
     * @return
     */
    @Override
    public List<ProductId> findProductIdList(String did, String clientId) {
        return backReturnOrderDao.findProductIdList(did, clientId);
    }

    /**
     * 查询商品信息
     * @param mid
     * @param productId
     * @return
     */
    @Override
    public Product findProduct(String mid, String productId) {
        return backReturnOrderDao.findProduct(mid, productId);
    }

    /**
     * 查询厂商简称
     * @param mid
     * @return
     */
    @Override
    public String findAbbreviation(String mid) {
        return backReturnOrderDao.findAbbreviation(mid);
    }
}
